<?php
namespace Tbf\Bundle\DatabaseBundle\ListQuery;
/**
 * 各种分页参数计算
 */
class Page {
    /**
     * 当前页码
     * @var integer
     */
    protected $page_num;
    /**
     * 每页数据条数
     * @var integer
     */
    protected $row_per_page;
    /**
     * 所有数据条数
     * @var integer
     */
    protected $total_row_num;
    /**
     * (分页栏)显示的页面数量
     * @var integer
     */
    protected $show_page_num;

    /**
     *
     * @param $page_num     当前页码
     * @param $page_size    每页数据条数
     * @param $total_result 所有数据条数
     */
    public function __construct($page_num,$row_per_page,$total_row_num){
        $this->page_num = $page_num;
        $this->row_per_page = $row_per_page;
        $this->total_row_num = $total_row_num;
    }

    /**
     * @param int $page_num
     */
    public function setPageNum($page_num)
    {
        $this->page_num = $page_num;
    }

    /**
     * 当前页码
     * @return int
     */
    public function getPageNum()
    {
        return $this->page_num;
    }

    /**
     * @param int $page_size
     */
    public function setRowPerPage($row_per_page)
    {
        $this->row_per_page = $row_per_page;
    }

    /**
     * 每页数据条数
     * @return int
     */
    public function getRowPerPage()
    {
        return $this->row_per_page;
    }

    /**
     * @param int $total_row_num
     */
    public function setTotalRowNum($total_row_num)
    {
        $this->total_row_num = $total_row_num;
    }

    /**
     * 所有数据条数
     * @return int
     */
    public function getTotalRowNum()
    {
        return $this->total_row_num;
    }

    /**
     * @param int $show_page_num
     */
    public function setShowPageNum($show_page_num)
    {
        $this->show_page_num = $show_page_num;
    }

    /**
     * (分页栏)显示的页码数量,默认10
     * @return int
     */
    public function getShowPageNum()
    {
        if ($this->show_page_num){
            return $this->show_page_num;
        }else{
            return 10;
        }
    }

    /**
     * 页面总数
     * @return int
     */
    public function getPageTotal(){
        return ceil($this->getTotalRowNum()/$this->getRowPerPage());
    }

    /**
     * mysql的那个limit语句
     */
    public function getMysqLimit(){
        return ($this->getPageNum()-1) * $this->getRowPerPage() .','.
            $this->getRowPerPage();
    }

    /**
     * 下一页的页码
     * @return int
     */
    public function getPageNumNext(){
        if ($this->getPageNum()+1 > $this->getPageTotal()){
            return $this->getPageTotal();
        }
        return $this->getPageNum()+1;
    }

    /**
     * (分页栏)起始页码
     */
    public function getPageNumFrom(){
        if ($this->getPageNum() <= $this->getShowPageNum()-1){
            return 1;
        }
        if ($this->getPageNum()+ floor($this->getShowPageNum()/2) >= $this->getPageTotal()){
            return $this->getPageTotal() - $this->getShowPageNum();
        }
        return $this->getPageNum() - ceil($this->getShowPageNum()/2);
    }

    /**
     * (分页栏)结束页码
     */
    public function getPageNumTo(){
        if ($this->getShowPageNum() > $this->getPageTotal()){
            return $this->getPageTotal();
        }
        if ($this->getPageNum() <= $this->getShowPageNum()-1){
            return $this->getShowPageNum();
        }
        if ($this->getPageNum()+ floor($this->getShowPageNum()/2) >= $this->getPageTotal()){
            return $this->getPageTotal();
        }
        return $this->getPageNum() + floor($this->getShowPageNum()/2);
    }
    public function getPageNumFromToArray(){
        return range($this->getPageNumFrom(),$this->getPageNumTo());
    }

    /**
     * (分页栏)显示页码块前是否有省略号
     * @return bool
     */
    public function hasPrev(){
        if ($this->getPageNumFrom()===1){
            return false;
        }else{
            return true;
        }
    }

    /**
     * (分页栏)显示页码块后是否有省略号
     */
    public function hasNext(){
        if ($this->getPageNumTo()===$this->getShowPageNum()){
            return false;
        }
        if ($this->getPageNumTo()===$this->getPageTotal()){
            return false;
        }
        return true;
    }
}